//
//  WCNHospitalListVC.m
//  WCN
//
//  Created by wsj2015 on 16/3/23.
//  Copyright © 2016年 ShanghaiCize Trade And Business Co., Ltd. All rights reserved.
//

#import "WCNHospitalListVC.h"
#import "WCNSearchResultsVC.h"
#import "WCHospital.h"
#import "WCHospitalCell.h"
@interface WCNHospitalListVC ()<UISearchBarDelegate>

@property (nonatomic,  strong) NSMutableArray *dataArray;
@property (nonatomic,  strong) UISearchBar *searchBar;

@end

@implementation WCNHospitalListVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor =kAppBg;
    [self configUI];
    [self postHospitalData];
}
- (NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray =[NSMutableArray array];
    }
    return _dataArray;
}
- (UISearchBar *)searchBar{
    if (!_searchBar) {
        _searchBar =[[UISearchBar alloc] init];
        _searchBar.frame = CGRectMake(0, 0, kScreenWidth, 44);
        _searchBar.delegate = self;
//        _searchBar.showsCancelButton = YES;
        _searchBar.barTintColor = [UIColor whiteColor];
       _searchBar.placeholder = @"请输护理师姓名或者ID";
        _searchBar.searchBarStyle = UISearchBarIconSearch;
        _searchBar.showsScopeBar = YES;
        
        
        
    }
    return _searchBar;
}
- (void)configUI{
    self.tableView.frame = CGRectMake(0, 44, kScreenWidth, kScreenHeight -44-64);
    
    self.title = @"选择医院";
    [self.view addSubview:self.searchBar];
    [self.tableView registerNib:[UINib nibWithNibName:@"WCHospitalCell" bundle:nil] forCellReuseIdentifier:@"hospitalCellId"];
    defineWeakSelf;
    self.tapGblock = ^(){
        [weakSelf.searchBar resignFirstResponder];
    };
    [self.tableView gzwLoading:^{
        [weakSelf postHospitalData];
    }];
}
- (void)postHospitalData{
    
    defineWeakSelf;
    
    self.tableView.loading = YES;
    CoreSVPLoading(@"加载中..", NO);
    [[WCNetworkAccessManager sharedMg] POST:kSearchHospitalList parameters:nil success:^(NSDictionary * responseObject) {
        if ([responseObject[@"data"] isKindOfClass:[NSArray class]]) {
          [weakSelf.dataArray addObjectsFromArray:[WCHospital objectArrayWithKeyValuesArray:responseObject[@"data"]]];
            [weakSelf.tableView reloadData];
        }
        
    } failure:nil netError:nil always:^{
        self.tableView.loading = NO;
        [CoreSVP dismiss];
    }];
  
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 64;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    WCHospitalCell *cell =[tableView dequeueReusableCellWithIdentifier:@"hospitalCellId" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    [cell configWith:self.dataArray[indexPath.row]];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    defineWeakSelf;
    WCHospital *model =self.dataArray[indexPath.row];
    WCNSearchResultsVC *vc = [WCNSearchResultsVC new];
    if (self.unableSelectArray) {
        vc.unableArray = self.unableSelectArray;
    }
    if (self.chooseType) {
        vc.chooseType = self.chooseType;
    }
    vc.onlySelectOne= self.onlySelectOne;
    vc.type = WCNSearchResultsVCSelectTypeMuti;
    vc.hospitalId =model.wcId;
    vc.popCallBack = ^(NSArray *selectItem){
        if (weakSelf.popCallBack) {
            weakSelf.popCallBack(selectItem);
//            [weakSelf.navigationController popViewControllerAnimated:YES];
        }
    };
    if ([self.searchBar isFirstResponder]) {
        self.searchBar.text = @"";
        [self.searchBar resignFirstResponder];
    }
    
    [self.navigationController pushViewController:vc animated:YES];
    
}
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
    
    
    defineWeakSelf;
    WCNSearchResultsVC *vc = [WCNSearchResultsVC new];
     vc.onlySelectOne= self.onlySelectOne;
    vc.popCallBack = ^(NSArray *selectItems){
        if (weakSelf.popCallBack) {
            weakSelf.popCallBack(selectItems);
            
            
//            [weakSelf.navigationController popViewControllerAnimated:YES];
        }
    };
    if (self.chooseType) {
        vc.chooseType = self.chooseType;
    }
    vc.type = WCNSearchResultsVCSelectTypeMuti;
    vc.unableArray = self.unableSelectArray;
    vc.keyWord = searchBar.text;
    searchBar.text = @"";
    [searchBar resignFirstResponder];
    [self.navigationController pushViewController:vc animated:YES];
}


@end
